package vcp.gui.secure.screens;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import vcp.gui.components.VCPPanel;
import vcp.gui.components.VCPTable;
import vcp.gui.forms.secure.CustomerContactForm;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ScreenContactCustomer extends VCPPanel implements ActionListener {
	private VCPTable table;
	private JScrollPane tabPane;
	private CustomerContactForm contactForm;

	/**
	 * Create the panel.
	 */
	public ScreenContactCustomer() {

		String[] columnNames = { "Customer ID", "First Name", "Last Name" };

		Object[][] data = { { new Integer(12345678), "ldkfvh", "sdckljh" }, };
		tabPane = new JScrollPane(getTable(columnNames, data));

		contactForm = new CustomerContactForm();
		contactForm.setVisible(false);

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout
				.createParallelGroup(Alignment.LEADING)
				.addComponent(tabPane, GroupLayout.PREFERRED_SIZE, 454,
						GroupLayout.PREFERRED_SIZE)
				.addComponent(contactForm, GroupLayout.PREFERRED_SIZE,
						GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addGap(1)
						.addComponent(tabPane, GroupLayout.PREFERRED_SIZE, 174,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(contactForm, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(174, Short.MAX_VALUE)));
		setLayout(groupLayout);

	}

	private VCPTable getTable(String[] columnNames, Object[][] data) {
		if (table == null) {
			table = new VCPTable(data, columnNames); 

			table.setPreferredScrollableViewportSize(new Dimension(500, 70));
			table.setFillsViewportHeight(true);
			table.getSelectionModel().addListSelectionListener(
					new RowListener());

			return table;
		}
		return null;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}

	private class RowListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent event) {
			if (event.getValueIsAdjusting()) {
				return;
			}
			showContactForm();

		}

		
	}
	
	private void showContactForm() {
		contactForm.setVisible(true);			
		int toID = (int) getSelectedValueInCol(0);
		// debug
		System.out.println(toID);
		
		contactForm.setToID(toID);
		contactForm.getParentFrame().pack();			
	}

	
	/*
	 * private class ColumnListener implements ListSelectionListener { public
	 * void valueChanged(ListSelectionEvent event) { if
	 * (event.getValueIsAdjusting()) { return; }
	 * output.append("COLUMN SELECTION EVENT. "); outputSelection(); } }
	 */

	private Object getSelectedValueInCol(int col) {
		int row = table.getSelectedRow();
		return table.getModel().getValueAt(row, col);
	}
}
